//
//  HMPhotoBrowerViewController.swift
//  HMWeiBo
//
//  Created by Apple on 16/8/10.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

private let  HMPhotoBrowerViewControllerReuseIdentifier = "HMPhotoBrowerViewControllerReuseIdentifier"

class HMPhotoBrowerViewController: UIViewController {

    var pic_urls: [HMPhotoModel]?
    
    var index: Int?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setupUI()
        
        setupCollection()
    }

    private func setupUI(){
        
        view.addSubview(collectionView)
        view.addSubview(saveButton)
        view.addSubview(dissButton)
        
        // 设置约束
        
        collectionView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(view.snp_edges)
        }
        
        
        dissButton.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(view.snp_left).offset(20)
            make.bottom.equalTo(view.snp_bottom).offset(-50)
            
            make.width.equalTo(100)
            make.height.equalTo(30)
            
        }
        
        saveButton.snp_makeConstraints { (make) -> Void in
            
            make.right.equalTo(view.snp_right).offset(-20)
            make.bottom.equalTo(view.snp_bottom).offset(-50)
            
            make.width.equalTo(100)
            make.height.equalTo(30)
            
        }
    }

    //MARK:-- 按钮点击方法
    @objc private func save(){
        
    }
    
    @objc private func dissmiss(){
        
        dismissViewControllerAnimated(false) { () -> Void in
            
        }
    }
   
    //MARK:-- 设置UICollectionView的属性
    private func setupCollection(){
        
        // 数据源
        collectionView.dataSource = self
        
        // 分页
        collectionView.pagingEnabled = true
        
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.showsVerticalScrollIndicator = false
        
        // 注册cell
        collectionView.registerClass(HMPhotoBrowerCollectionViewCell.self, forCellWithReuseIdentifier: HMPhotoBrowerViewControllerReuseIdentifier)
        
        
        flowLayout.itemSize = CGSizeMake(kUIScreenWidth, KUIScreenHeight)
        
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.minimumLineSpacing = 0
        
        // 设置滚动方向
        flowLayout.scrollDirection = .Horizontal
        
    }
    
    //MARK:-- 懒加载
    var flowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    
    private lazy var collectionView: UICollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: self.flowLayout)
    
    // 创建2个按钮
    private lazy var saveButton: UIButton =  UIFactory.getButton("保存", textColor: UIColor.whiteColor(), image: nil, bgImage: nil, target: self, action: "save")
    
    private lazy var dissButton: UIButton = UIFactory.getButton("关闭", textColor: UIColor.whiteColor(), image: nil , bgImage: nil, target: self, action: "dissmiss")

}

extension HMPhotoBrowerViewController: UICollectionViewDataSource{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pic_urls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(HMPhotoBrowerViewControllerReuseIdentifier, forIndexPath: indexPath) as! HMPhotoBrowerCollectionViewCell
        
     cell.photoModel = pic_urls![indexPath.item]
        cell.backgroundColor = RandomColor()
        
        return cell
        
    }
}
